package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.IntegralMallAdapter;
import com.dingwei.superiordiary.bean.CommodityDetailsBean;
import com.dingwei.superiordiary.bean.IntegerMallBean;
import com.dingwei.superiordiary.mvp.presenter.IntegerMallPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.utils.Utils;
import com.lihao.baselibrary.weight.TitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.umeng.socialize.media.Base;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/2/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class IntegerMallActivity extends BaseActivity implements Contract.IntegalMallView {

    IntegerMallPresenter integerMallPresenter = new IntegerMallPresenter(this);

    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    IntegralMallAdapter adapter;
    int page = 1;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_integral_mall;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {

        adapter = new IntegralMallAdapter(BaseActivity.context);

        GridLayoutManager layoutManager = new GridLayoutManager(BaseActivity.context, 2);
        layoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(2));
        SpaceDecoration itemDecoration = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 16));//参数是距离宽度
        itemDecoration.setPaddingEdgeSide(true);//是否为左右2边添加padding.默认true.
        itemDecoration.setPaddingStart(true);//是否在给第一行的item添加上padding(不包含header).默认true.
        itemDecoration.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(itemDecoration);


    }

    @Override
    protected void initListener() {

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                integerMallPresenter.initData(page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                integerMallPresenter.initData(page, true, false);
            }
        });

        adapter.xxInterfaceListener(new IntegralMallAdapter.ExchangeInterFace() {
            @Override
            public void exchange(int position) {
                CommodityDetailsBean.DataBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context,FirmOrderActivity.class);
                intent.putExtra("bean",bean);
                intent.putExtra("id",bean.id);
                intent.putExtra("num",1);
                startActivity(intent);
            }
        });
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                CommodityDetailsBean.DataBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context,IntegerDetailsActivity.class);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh(IntegerMallBean bean) {
        adapter.clear();
        adapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void onLoadMore(IntegerMallBean bean) {
        adapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            mRecyclerView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            noDataLayout.setVisibility(View.VISIBLE);
        }
    }
}
